export async function generateCrudPage(config) {
  const {
    apiUrl,
    headerToolbar,
    bulkActions,
    filter,
    showIndex,
    draggable,
    itemCheckableOn,
    columns,
  } = config;

  const body = {
    type: "crud",
    perPage: 20,
    alwaysShowPagination: true,//是否总是显示分页
    affixHeader: false,//是否固定表头


    filterTogglable: true,//是否可切换筛选
    filterDefaultVisible: false,//是否默认显示筛选
    api: apiUrl+"?_action=getData",
    quickSaveApi: `post:${apiUrl}?_action=quickEdit`,
    quickSaveItemApi: `post:${apiUrl}?_action=quickEditItem`,
    saveOrderApi: `post:${apiUrl}?_action=saveOrder`,//保存排序的API地址
    syncLocation: false,
    itemCheckableOn: itemCheckableOn,//是否可勾选
    columns: columns || [],
  };
  //表头工具栏配置
  if (headerToolbar) {
    body.headerToolbar = headerToolbar;
  }
  //批量操作配置
  if (bulkActions) {
    body.bulkActions = bulkActions;
  }
  //筛选配置
  if (filter) {
    body.filter = filter;
  }
  //是否显示序号列
  if (showIndex) {
    body.showIndex = showIndex;
  }
  //是否可拖拽排序
  if (draggable) {
    body.draggable = draggable;
  }

  return {
    type: "page",
    className: "m:overflow-auto",
    body,
  };
}

//数据列表操作按钮配置
export async function headerToolbar(apiUrl, form) {
  return [
    {
      reload: true,
      label: "",
      type: "button",
      actionType: "reload",
      icon: "fa fa-repeat",
      align: "right"
    },
    {
      label: "新增",
      icon: "fa fa-add",
      type: "button",
      level: "primary",
      actionType: "dialog",
      dialog: {
        title: "新增",
        body: {
          type: "form",
          api: `post:${apiUrl}`,
          body: await form(),
        },
      },
    },
    {
      type: "filter-toggler",
      align: "right"
    },
    "bulkActions",
    "export-excel",
  ];
}

//数据列表操作按钮配置
export async function listButtons({ types = [], apiUrl, detail, form, buttonsOptions = {},button1=()=>{return {}} }) {
  const buttons = [];
  if (types.includes("button1")) {
    buttons.push(await button1());
  }
  if (types.includes("edit")) {
    buttons.push({
      label: "编辑",
      type: "button",
      level: "link",
      actionType: "dialog",
      dialog: {
        title: "编辑",
        body: {
          type: "form",
          initApi:  apiUrl + "/${id}/edit?_action=getData",
          api: "put:" + apiUrl + "/${id}",
          body: await form(),
        },
      }
    });
  }

  if (types.includes("detail")) {
    buttons.push({
      label: "查看",
      type: "button",
      level: "link",
      actionType: "dialog",
      dialog: await detail()
    });
  }

  if (types.includes("delete")) {
    buttons.push({
      label: "删除",
      type: "button",
      level: "link",
      className: "text-danger",
      // @ts-ignore
      disabledOn: buttonsOptions.delete?.disabledOn || "false",
      actionType: "ajax",
      confirmText: "确认要删除该吗？",
      api: "delete:" + apiUrl + "/${id}"
    });
  }

  return {
    type: "operation",
    label: "操作",
    buttons: buttons
  };
}


//批量操作配置
export async function bulkActions(apiUrl) {
  return [
    {
      label: "删除",
      icon: "fa-solid fa-trash-can",
      type: "button",
      level: "danger",
      actionType: "ajax",
      api: "delete:" + apiUrl + "/${ids}",
      confirmText: "确认删除选中项？"
    }
  ];
}
export async function footerToolbar() {
  return [
    "statistics",
    {
      type: "form",
      wrapWithPanel: false,
      body: [
        {
          type: "select",
          name: "perPage",
          options: [
            {
              label: "20 条/页",
              value: 20
            },
            {
              label: "30 条/页",
              value: 30
            },
            {
              label: "50 条/页",
              value: 50
            },
            {
              label: "100 条/页",
              value: 100
            },
            {
              label: "200 条/页",
              value: 200
            }
          
          ],
        }
      ],
    }]
}
      
